<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="../utilities/master-calendar.xsl"/>
<xsl:import href="../utilities/date-and-time.xsl"/>
<xsl:import href="../utilities/pagination.xsl"/>


<xsl:template match="data">
	
	<div class="grid_16">
		<div class="grid_5 alpha">
			<dl id="apt_next">
				<dt>Next Appointment</dt>
				<xsl:choose>
					<xsl:when test="/data/future-client-appointments-short/error = 'No records found.'">
						<dd><em>No Appointments Yet</em></dd>
						<dd>Making an appointment is easy! Click the day of the month for the appointment, then select a desired time.</dd>
					</xsl:when>
					<xsl:otherwise>
						<xsl:for-each select="/data/future-client-appointments-short/entry[1]">
							<dd>
								<em>
									<xsl:call-template name="get-formatted-date">
										<xsl:with-param name="date" select="appointment-date"/>
										<xsl:with-param name="format-type" select="'short'"/>
									</xsl:call-template>
									<xsl:text>, </xsl:text>
									<xsl:call-template name="get-formatted-time">
										<xsl:with-param name="time" select="appointment-date/@time"/>
									</xsl:call-template>
								</em>
							</dd>
						</xsl:for-each>
					</xsl:otherwise>
				</xsl:choose>
			</dl>
			<xsl:choose>
				<xsl:when test="not(/data/past-client-appointments-short/error)">
					<dl id="apts_past">
						<dt>Past Appointments</dt>
						<xsl:for-each select="/data/past-client-appointments-short/entry[appointment-date != $today]">
							<dd>
								<img src="{$workspace}/css/img/icon_{payment-status/item/@handle}.gif" title="{payment-status/item}" alt="{payment-status/item}"/>
								<em>
									<!--a href="{$root}/archives/" title="Appointment {payment-status/item}"-->
									<xsl:call-template name="get-formatted-date">
										<xsl:with-param name="date" select="appointment-date"/>
										<xsl:with-param name="format-type" select="'short'"/>
									</xsl:call-template>
									<xsl:text>, </xsl:text>
									<xsl:call-template name="get-formatted-time">
										<xsl:with-param name="time" select="appointment-date/@time"/>
									</xsl:call-template>
								</em>
							</dd>
						</xsl:for-each>
					</dl>
				</xsl:when>
			</xsl:choose>
		</div>
		<div class="grid_11 omega" id="archives">
			<h2>Past Appointments Details</h2>
			<xsl:choose>
				<xsl:when test="past-client-appointments-long/error = 'No records found.'">
					<p>You have no past appointments yet. Once you do, you'll be able to view them here.</p>
				</xsl:when>
				<xsl:otherwise>
					<p>Your current outstanding balance is: 
						<em id="client_balance_1"></em>
					</p>
				    <table id="pricingTable" cellpadding="0" cellspacing="0">
				        <thead>
				        <tr>
				            <th class="sortBy">Appointment <br />Date</th>
				            <th>Payment <br />Status</th>
				            <th>Payment <br />Method</th>
				            <th>Charge</th>
				            <th>Insurance <br />Coverage</th>
				            <th>Patient <br />Responsibility</th>
				            <th>Patient <br />Payment</th>
				            <th>Balance</th>
				        </tr>
				        </thead>
				        <tbody>
							<xsl:apply-templates select="past-client-appointments-long/entry" mode="tr"/>
				        </tbody>
				    </table>
				    <table id="balance_total" cellpadding="0" cellspacing="0">
				        <tbody>
							<tr>
								<td colspan="7">Balance Total</td>
								<td id="client_balance_2"></td>
							</tr>
				        </tbody>
				    </table>
					<xsl:call-template name="outstanding-balance"/>
				</xsl:otherwise>
			</xsl:choose>
			<div id="pagination">
				<xsl:call-template name="pagination">
					<xsl:with-param name="pagination-element" select="past-client-appointments-long/pagination"/>
					<xsl:with-param name="url" select="$root"/>
					<xsl:with-param name="display-number" select="5"/>
				</xsl:call-template>
			</div>
		</div>
	</div>
</xsl:template>

<xsl:template name="outstanding-balance">
	<script type="text/javascript">
		var total = <xsl:apply-templates select="past-client-appointments-long/entry" mode="balance"/>;
		total = "$" + (total).numberFormat(2, '.', ',');
		$('client_balance_1').set('html',total);
		$('client_balance_2').set('html',total);
	</script>
</xsl:template>
<xsl:template match="past-client-appointments-long/entry" mode="balance">
	<xsl:choose>
		<xsl:when test="payment-status/item/@handle = 'pending'">
			<xsl:text>0</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="number(balance)"/>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:if test="position() &lt; last()">
		<xsl:text>+</xsl:text>
	</xsl:if>
</xsl:template>

<xsl:template match="past-client-appointments-long/entry" mode="tr">
	<tr class="allUserPrices" id="{@id}">
		<xsl:apply-templates select="." mode="td"/>
	</tr>
</xsl:template>

<xsl:template match="past-client-appointments-long/entry" mode="td">
	<td class="appointment-date">
		<xsl:value-of select="appointment-date"/>
		<!--xsl:call-template name="get-formatted-date">
			<xsl:with-param name="date" select="appointment-date"/>
			<xsl:with-param name="format-type" select="'short'"/>
		</xsl:call-template-->
		<xsl:text>, </xsl:text>
		<xsl:call-template name="get-formatted-time">
			<xsl:with-param name="time" select="appointment-date/@time"/>
		</xsl:call-template>
	</td>
	<td class="payment-status-{payment-status/item/@handle}">
		<xsl:value-of select="payment-status/item"/>
	</td>
	<td class="pay_status_method">
		<xsl:value-of select="payment-method/item"/>
	</td>
	<td>
		<xsl:value-of select="charge"/>
	</td>
	<td>
		<xsl:value-of select="insurance-payment"/>
	</td>
	<td>
		<xsl:value-of select="patient-responsibility"/>
	</td>
	<td>
		<xsl:value-of select="patient-payment"/>
	</td>
	<td>
		<xsl:value-of select="balance"/>
	</td>
</xsl:template>

</xsl:stylesheet>